package cn.com.connected.service.impl;

import cn.com.connected.base.cache.CacheService;
import cn.com.connected.common.emnus.CacheKey;
import cn.com.connected.common.utils.HttpUtil;
import cn.com.connected.service.WeChatCommonService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: service-server
 * @description: 微信 获取用户信息
 * @author: 邵明生
 * @create: 2019-03-25 14:01
 **/
@Slf4j
@Service
public class WeChatCommonServiceImpl implements WeChatCommonService {

    @Resource
    private CacheService cacheService;

    @Resource
    private HttpUtil httpUtil;

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    @Value("${wechat.url.jscode2session}")
    private String jscode2session;

    @Value("${wechat.url.token}")
    private String tokenUrl;

    @Value("${wechat.url.getuserphonenumber}")
    private String phoneNumberUrl;

    @Value("${wechat.url.msg_sec_check}")
    private String msgSecCheckUrl;

    @Value("${wechat.url.media_check_async}")
    private String mediaCheckAsync;

    @Override
    public String wxLogin(String code) {
        log.info("微信获取openID appId:{} appSecret:{} code:{}",appId,appSecret,code);
        JSONObject jsonObject = httpUtil.get(String.format(jscode2session, appId, appSecret, code), JSONObject.class);
        return jsonObject.getString("openid");
    }

    @Override
    public String getAccessToken() {
        String accessToken = cacheService.getObject(CacheKey.WX_ACCESS_TOKEN);
        if (StringUtils.isEmpty(accessToken)){
            JSONObject jsonObject = httpUtil.get(String.format(tokenUrl, appId, appSecret), JSONObject.class);
            accessToken =  jsonObject.getString("access_token");
            int expiresIn =  jsonObject.getIntValue("expires_in");
            cacheService.putObject(CacheKey.WX_ACCESS_TOKEN,accessToken,expiresIn-5*CacheService.EXPIRE_SECOND);
        }
        return accessToken;
    }

    @Override
    public String phoneNumber(String code) {
        // 获取token
        String token = this.getAccessToken();

        // 使用前端code获取手机号码 参数为json格式
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("code", code);

        JSONObject jsonObject = httpUtil.post(String.format(phoneNumberUrl, token),paramMap,JSONObject.class);

        return jsonObject.getJSONObject("phone_info").getString("phoneNumber");

    }

    @Override
    public void msgSecCheck(String content) {
        // 获取token
        String token = this.getAccessToken();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("content", content);

        httpUtil.post(String.format(msgSecCheckUrl, token),paramMap,JSONObject.class);
    }

    @Override
    public Map<String,String> mediaCheckAsync(List<String> imgs,String openid) {
//        if (CollectionUtils.isEmpty(imgs)){
//            return Collections.emptyMap();
//        }
//        Map<String,String> results = new HashMap<>(imgs.size());
//
//        String token = this.getAccessToken();
//        Map<String, String> paramMap = new HashMap<>();
//        for (String img : imgs) {
//            AttrVO attr = ossService.authorize(Arrays.asList(img), FileType.DYNAMIC_IMG).get(0);
//            log.info("提交检测图片 {}",attr.getPrimary());
//            paramMap.put("media_url", attr.getPrimary());
//            paramMap.put("media_type", "2");//1:音频;2:图片
//            paramMap.put("version", "2");
//            paramMap.put("openid", openid);
//            paramMap.put("scene", "4");
//
//            JSONObject object = httpUtil.post(String.format(mediaCheckAsync, token),paramMap,JSONObject.class);
//            log.info(object.toJSONString());
//
//            results.put(object.getString(check_flag),img);
//        }
//
//        return results;
        return null;
    }
}

